package com.qingcloud.task.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult<T> implements Serializable {

  public static <T> JsonResult<T> success(T object) {
    return new JsonResult(SUCCEED_CODE, "", object);
  }

  public static JsonResult success() {
    return new JsonResult(SUCCEED_CODE, "", null);
  }

  public static JsonResult fail(int code, String message) {
    return new JsonResult(code, message, null);
  }

  public static JsonResult fail(String message) {
    return new JsonResult(FAIL_CODE, message, null);
  }

  public static JsonResult fail() {
    return new JsonResult(FAIL_CODE, "系统繁忙请稍后重试", null);
  }

  public static JsonResult noLogin(String message) {
    return new JsonResult(NO_LOGIN, message, null);
  }

  public static JsonResult noLogin() {
    return new JsonResult(NO_LOGIN, "请登录", null);
  }

  public JsonResult(int code, String message) {
    this.erroCode = code;
    this.erroMsg = message;
  }

  public final static int SUCCEED_CODE = 2000;//正常

  public final static int FAIL_CODE = -1;//异常

  public final static int NO_LOGIN = 0;//未登录

  @ApiModelProperty(value = "返回码 2000正常 其他错误", example = "2000")
  protected int erroCode;

  @ApiModelProperty(value = "异常信息 2000正常 其他错误", example = "网络错误")
  protected String erroMsg;

  @ApiModelProperty(value = "AJAX结果数据", example = "网络错误")
  private T result;

}
